﻿using Dapper;
using System;
using BCEntity.Trading.Enquiry.SaleEnquiryChecklist;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Threading.Tasks;
using BCCommon;

namespace BCData.Trading.Enquiry.SaleEnquiryChecklist
{
    /// <summary>
    /// 销售询盘清单
    /// </summary>
    public class SaleEnquiryChecklistData : ISaleEnquiryChecklistData
    {
        private readonly IDatabaseContext databaseContext;
        private const string QUERY_EXISTS = @"SELECT 1 FROM saleenquirychecklist";
        private const string QUERY = @"SELECT SaleEnquiryChecklistId,NormName,NormNo,MainImage,SaleAmount,SaleUnit,SaleCount,CreateTime,SaleEnquiryId,SaleShoppingCartId,SaleGoodsCategoryId,CategoryName,SaleGoodsId,GoodsName,SKU,SaleGoodsNormId FROM saleenquirychecklist";
        public SaleEnquiryChecklistData(IDatabaseContext databaseContext)
        {
            this.databaseContext = databaseContext;
        }
        #region 检查销售询盘清单是否存在
        public async Task<bool> Exists(long saleEnquiryChecklistId)
        {
            string sql = string.Format(@"{0} WHERE SaleEnquiryChecklistId=?SaleEnquiryChecklistId", QUERY_EXISTS);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?SaleEnquiryChecklistId", saleEnquiryChecklistId, DbType.Int64);
            return await this.databaseContext.Slave.ExecuteScalarAsync(sql, parameters) != null;
        }
        public async Task<bool> Exists(long saleEnquiryId, long saleGoodsNormId)
        {
            string sql = string.Format(@"{0} WHERE SaleEnquiryId=?SaleEnquiryId AND SaleGoodsNormId=?SaleGoodsNormId", QUERY_EXISTS);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?SaleEnquiryId", saleGoodsNormId, DbType.Int64);
            parameters.Add("?SaleGoodsNormId", saleGoodsNormId, DbType.Int64);
            return await this.databaseContext.Slave.ExecuteScalarAsync(sql, parameters) != null;
        }
        #endregion

        #region 添加销售询盘清单
        public async Task<SaleEnquiryChecklistEntity> Add(SaleEnquiryChecklistEntity entity)
        {
            string sql = SqlCommands.GetInsertSql("saleenquirychecklist", new string[] { "NormName", "NormNo", "MainImage", "SaleAmount", "SaleUnit", "SaleCount", "SaleEnquiryId",
                "SaleShoppingCartId", "SaleGoodsCategoryId", "CategoryName", "SaleGoodsId", "GoodsName", "SKU", "SaleGoodsNormId" });
            sql = string.Format("{0} {1} WHERE SaleEnquiryChecklistId=LAST_INSERT_ID();", sql, QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?SaleUnit", entity.SaleUnit?.Trim(), DbType.AnsiString, size: 10);
            parameters.Add("?NormName", entity.NormName?.Trim(), DbType.AnsiString, size: 50);
            parameters.Add("?NormNo", entity.NormNo?.Trim(), DbType.AnsiString, size: 100);
            parameters.Add("?MainImage", entity.MainImage?.Trim(), DbType.AnsiString, size: 1000);
            parameters.Add("?SaleAmount", entity.SaleAmount, DbType.Decimal, size: 16);
            parameters.Add("?SaleUnit", entity.SaleUnit?.Trim(), DbType.AnsiString, size: 10);
            parameters.Add("?SaleCount", entity.SaleCount, DbType.Int32, size: 11);
            parameters.Add("?SaleEnquiryId", entity.SaleEnquiryId, DbType.Int64, size: 20);
            parameters.Add("?SaleShoppingCartId", entity.SaleShoppingCartId, DbType.Int64, size: 20);
            parameters.Add("?SaleGoodsCategoryId", entity.SaleGoodsCategoryId, DbType.Int64, size: 20);
            parameters.Add("?CategoryName", entity.CategoryName?.Trim(), DbType.AnsiString, size: 10);
            parameters.Add("?SaleGoodsId", entity.SaleGoodsId, DbType.Int64, size: 20);
            parameters.Add("?GoodsName", entity.GoodsName?.Trim(), DbType.AnsiString, size: 50);
            parameters.Add("?SKU", entity.Sku?.Trim(), DbType.AnsiString, size: 50);
            parameters.Add("?SaleGoodsNormId", entity.SaleGoodsNormId, DbType.Int64, size: 20);
            return await this.databaseContext.Master.QueryFirstOrDefaultAsync<SaleEnquiryChecklistEntity>(sql, parameters, databaseContext.Transaction);

        }
        #endregion

        #region 修改销售询盘清单
        public async Task<SaleEnquiryChecklistEntity> Update(SaleEnquiryChecklistEntity entity)
        {
            string sql = SqlCommands.GetUpdateSql("saleenquirychecklist", new string[] { "NormName", "NormNo", "MainImage", "SaleAmount", "SaleUnit", "SaleCount", "SaleEnquiryId",
                "SaleShoppingCartId", "SaleGoodsCategoryId", "CategoryName", "SaleGoodsId", "GoodsName", "SKU", "SaleGoodsNormId" }, new string[] { "SaleEnquiryChecklistId" });
            sql = string.Format("{0} {1} WHERE SaleEnquiryChecklistId=?SaleEnquiryChecklistId;", sql, QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?SaleEnquiryChecklistId", entity.SaleEnquiryChecklistId, DbType.Int64, size: 20);
            parameters.Add("?NormName", entity.NormName?.Trim(), DbType.AnsiString, size: 50);
            parameters.Add("?NormNo", entity.NormNo?.Trim(), DbType.AnsiString, size: 100);
            parameters.Add("?MainImage", entity.MainImage?.Trim(), DbType.AnsiString, size: 1000);
            parameters.Add("?SaleAmount", entity.SaleAmount, DbType.Decimal, size: 16);
            parameters.Add("?SaleUnit", entity.SaleUnit?.Trim(), DbType.AnsiString, size: 10);
            parameters.Add("?SaleCount", entity.SaleCount, DbType.Int32, size: 11);
            parameters.Add("?SaleEnquiryId", entity.SaleEnquiryId, DbType.Int64, size: 20);
            parameters.Add("?SaleShoppingCartId", entity.SaleShoppingCartId, DbType.Int64, size: 20);
            parameters.Add("?SaleGoodsCategoryId", entity.SaleGoodsCategoryId, DbType.Int64, size: 20);
            parameters.Add("?CategoryName", entity.CategoryName?.Trim(), DbType.AnsiString, size: 10);
            parameters.Add("?SaleGoodsId", entity.SaleGoodsId, DbType.Int64, size: 20);
            parameters.Add("?GoodsName", entity.GoodsName?.Trim(), DbType.AnsiString, size: 50);
            parameters.Add("?SKU", entity.Sku?.Trim(), DbType.AnsiString, size: 50);
            parameters.Add("?SaleGoodsNormId", entity.SaleGoodsNormId, DbType.Int64, size: 20);
            return await this.databaseContext.Master.QueryFirstOrDefaultAsync<SaleEnquiryChecklistEntity>(sql, parameters, databaseContext.Transaction);
        }
        #endregion

        #region 删除销售询盘清单
        public async Task<bool> Delete(long saleEnquiryChecklistId)
        {
            string sql = @"DELETE FROM saleenquirychecklist WHERE SaleEnquiryChecklistId=?SaleEnquiryChecklistId";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?SaleEnquiryChecklistId", saleEnquiryChecklistId, DbType.Int64);
            return await this.databaseContext.Master.ExecuteAsync(sql, parameters, databaseContext.Transaction) > 0;
        }
        public async Task<bool> DeleteEnquiry(long saleEnquiryId)
        {
            string sql = @"DELETE FROM saleenquirychecklist WHERE SaleEnquiryId=?SaleEnquiryId";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?SaleEnquiryId", saleEnquiryId, DbType.Int64);
            return await this.databaseContext.Master.ExecuteAsync(sql, parameters, databaseContext.Transaction) > 0;
        }
        #endregion

        #region 增加询盘清单销售商品数量
        public async Task<SaleEnquiryChecklistEntity> IncreaseSaleCount(long saleEnquiryChecklistId)
        {

            string sql = string.Format(@"UPDATE saleenquirychecklist SET SaleCount=SaleCount+1 WHERE SaleEnquiryChecklistId=?SaleEnquiryChecklistId;
                {0} WHERE SaleEnquiryChecklistId=?SaleEnquiryChecklistId;", QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?SaleEnquiryChecklistId", saleEnquiryChecklistId, DbType.Int64, size: 20);
            return await this.databaseContext.Master.QueryFirstOrDefaultAsync<SaleEnquiryChecklistEntity>(sql, parameters, databaseContext.Transaction);
        }
        #endregion

        #region 减少询盘清单销售商品数量
        public async Task<SaleEnquiryChecklistEntity> ReduceSaleCount(long saleEnquiryChecklistId)
        {
            string sql = string.Format(@"UPDATE saleenquirychecklist SET SaleCount=SaleCount-1 WHERE SaleEnquiryChecklistId=?SaleEnquiryChecklistId;
                {0} WHERE SaleEnquiryChecklistId=?SaleEnquiryChecklistId;", QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?SaleEnquiryChecklistId", saleEnquiryChecklistId, DbType.Int64, size: 20);
            return await this.databaseContext.Master.QueryFirstOrDefaultAsync<SaleEnquiryChecklistEntity>(sql, parameters, databaseContext.Transaction);
        }
        #endregion

        #region 获取销售询盘清单
        public async Task<SaleEnquiryChecklistEntity> GetEntity(long saleEnquiryId, long saleGoodsNormId)
        {
            string sql = string.Format(@"{0} WHERE SaleEnquiryId=?SaleEnquiryId AND SaleGoodsNormId=?SaleGoodsNormId", QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?SaleEnquiryId", saleGoodsNormId, DbType.Int64);
            parameters.Add("?SaleGoodsNormId", saleGoodsNormId, DbType.Int64);
            return await this.databaseContext.Slave.QueryFirstOrDefaultAsync<SaleEnquiryChecklistEntity>(sql, parameters);
        }

        public async Task<SaleEnquiryChecklistEntity> GetEntity(long saleEnquiryChecklistId)
        {
            string sql = string.Format(@"{0} WHERE SaleEnquiryChecklistId=?SaleEnquiryChecklistId", QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?SaleEnquiryChecklistId", saleEnquiryChecklistId, DbType.Int64);
            return await this.databaseContext.Slave.QueryFirstOrDefaultAsync<SaleEnquiryChecklistEntity>(sql, parameters);
        }
        #endregion

        #region 获取销售询盘清单列表
        public async Task<IEnumerable<SaleEnquiryChecklistEntity>> GetList(long saleEnquiryId)
        {
            string sql = string.Format(@"{0} WHERE SaleEnquiryId=?SaleEnquiryId", QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?SaleEnquiryId", saleEnquiryId, DbType.Int64);
            return await this.databaseContext.Slave.QueryAsync<SaleEnquiryChecklistEntity>(sql,parameters);
        }
        #endregion

        #region 获取销售询盘清单分页
        public async Task<(IEnumerable<SaleEnquiryChecklistEntity>, int)> Query(long? saleEnquiryId,DateTime? start, DateTime? end, string sortName, bool? ascending, int pageIndex, int pageSize)
        {
            List<string> wheres = new List<string>();
            DynamicParameters parameters = new DynamicParameters();

            if (saleEnquiryId.HasValue)
            {
                wheres.Add("SaleEnquiryId=?SaleEnquiryId");
                parameters.Add("?SaleEnquiryId", saleEnquiryId.Value, DbType.Int64);
            }

            if (start.HasValue && !end.HasValue)
            {
                wheres.Add("CreateTime>=?Start");
                parameters.Add("?Start", start.Value, DbType.DateTime);
            }
            else if (!start.HasValue && end.HasValue)
            {
                wheres.Add("CreateTime<=?End");
                parameters.Add("?End", end.Value, DbType.DateTime);
            }
            else if (start.HasValue && end.HasValue)
            {
                wheres.Add("CreateTime>=?Start AND CreateTime<=?End");
                parameters.Add("?Start", start.Value, DbType.DateTime);
                parameters.Add("?End", end.Value, DbType.DateTime);
            }
            string sql = string.Format(@"{0} {1} ORDER BY {4} {5} LIMIT {2},{3}; SELECT COUNT(1) FROM saleenquirychecklist {1}",
                  QUERY,
                  wheres.Count > 0 ? " WHERE " + string.Join(" AND ", wheres) : "",
                  (pageIndex == 0 ? 0 : pageIndex - 1) * pageSize,
                  pageSize,
                  string.IsNullOrWhiteSpace(sortName) ? "CreateTime" : sortName,
                  ascending.HasValue && ascending.Value ? "ASC" : "DESC");
            return await this.databaseContext.Slave.QueryMultipleAsync<SaleEnquiryChecklistEntity, int>(sql, parameters);
        }
        #endregion
    }
}



